from typing import Optional, List

from fastapi import APIRouter, Depends, Body, Query
from sqlalchemy.orm import Session

from server.app.cloud_ide.model import IdeEnvironment
from server.app.cloud_ide.schema.ide_environment.request.ide_environment_create_request import \
    IdeEnvironmentCreateRequest
from server.app.cloud_ide.schema.ide_environment.request.ide_environment_update_request import \
    IdeEnvironmentUpdateRequest
from server.app.cloud_ide.service.ide_environment_service import IdeEnvironmentService
from server.framework.core.http_schemas import CommonResponseSchema, TableResponseSchema
from server.framework.core.injects import get_db
from server.framework.core.schemas import OptionsSchema

ide_environment_api = APIRouter()
ide_environment_service = IdeEnvironmentService()


@ide_environment_api.get("/get_entity", name='获取环境配置实体')
async def get_entity(id: str = Query(None, description='环境配置id'),
                     db: Session = Depends(get_db)) -> IdeEnvironment:
    entity = ide_environment_service.get_ide_environment_by_id(db, id)
    return entity


@ide_environment_api.get("/get_ide_environment_options", name='获取环境配置下拉选项')
async def get_ide_environment_options(db: Session = Depends(get_db)) -> List[OptionsSchema]:
    query = ide_environment_service.get_all_environments(db)

    option_results: List[OptionsSchema] = []
    for row in query:
        option_results.append(OptionsSchema(value=str(row.id), label=row.name))
    return option_results


@ide_environment_api.get("/list", response_model=TableResponseSchema, name='获取环境配置列表')
async def list(name: Optional[str] = Query(None, description='过滤条件:环境配置名称'),
               current: Optional[int] = Query(1, description='当前页数'),
               page_size: Optional[int] = Query(10, description='每页记录的数量', alias='pageSize'),
               db: Session = Depends(get_db)) -> TableResponseSchema:
    """
    获取环境配置列表
    :return:
    """
    items, total = ide_environment_service.get_ide_environment_list(db, name, page_size, current)
    return TableResponseSchema(data=items, total=total, page=page_size, success=True)


@ide_environment_api.post("/create", response_model=CommonResponseSchema, name='创建环境配置')
async def create(data: IdeEnvironmentCreateRequest = Body(None, description='环境配置创建实体'),
                 db: Session = Depends(get_db)) -> CommonResponseSchema:
    """
    创建开发环境配置
    :return:
    """
    rs = ide_environment_service.create_ide_environment(db, data)
    if rs:
        return CommonResponseSchema(data={}, message=f'创建成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'创建失败', success=False)


@ide_environment_api.get("/delete", response_model=CommonResponseSchema, name='删除环境配置')
async def delete(ids: str = Query(None, description='环境配置id，多条记录以逗号分割'),
                 db: Session = Depends(get_db)) -> CommonResponseSchema:
    """
    删除开发环境配置
    :return:
    """
    rs = ide_environment_service.delete_ide_environment(db, ids.split(','))
    if rs:
        return CommonResponseSchema(data={}, message=f'删除成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'删除失败', success=False)


@ide_environment_api.post("/edit", response_model=CommonResponseSchema, name='编辑环境配置')
async def edit(data: IdeEnvironmentUpdateRequest = Body(None, description='环境配置编辑实体'),
               db: Session = Depends(get_db)) -> CommonResponseSchema:
    """
    编辑开发环境配置
    :return:
    """
    rs = ide_environment_service.update_ide_environment(db, data)
    if rs:
        return CommonResponseSchema(data={}, message=f'编辑成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'编辑失败', success=False)
